﻿using System;
using System.Data;
using System.IO;
using System.Web;
using BLL;

namespace ERP
{
    public class Global : HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }
        protected void Session_Start(object sender, EventArgs e)
        {
            //Session.Timeout = 180;
            //Session["login"] = false;
            //Session["Users_Id"] = string.Empty;
            //Session["UserName"] = string.Empty;
            //Session["Passwords"] = string.Empty;
            //Session["Employee_Id"] = string.Empty;
            //Session["USer_Detail_Id"] = string.Empty;
            //Session["IsUsed"] = string.Empty;
            //Session["IsActive"] = string.Empty;


            //Session["login"] = true;
            //Session["Users_Id"] = 1;
            //Session["UserName"] = "manager";
            //Session["Employee_Id"] = 5;
            //Session["USer_Detail_Id"] = 1;
            //CheckXml();
            var cookie = Request.Cookies["login"];
            if (cookie != null)
            {
                Session["login"] = true;
                Session["Users_Id"] = cookie["Users_Id"];
                Session["UserName"] = cookie["UserName"];
                Session["Employee_Id"] = cookie["Employee_Id"];
                Session["USer_Detail_Id"] = cookie["USer_Detail_Id"];
            }
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }
        protected void Application_Error(object sender, EventArgs e)
        {

        }
        protected void Session_End(object sender, EventArgs e)
        {
            //try
            //{
            //    var obj = new EtUser { UsersId = int.Parse(Session["Users_Id"].ToString()) };
            //    BllUser.UpdateLastLogin(obj);
            //    Session.RemoveAll();
            //}
            //catch (Exception ex)
            //{
            //    HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            //}
            Session.RemoveAll();
        }
        protected void Application_End(object sender, EventArgs e)
        {

        }
        #region[Method]
        private void CheckXml()
        {
            var _clsUser = new BllUser();
            var _dtUser = _clsUser.GetUser(string.Empty);
            var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/");
            if (File.Exists(path + "Session.xml"))
            {
                var reader = System.Xml.XmlReader.Create(path + "Session.xml");
                var _ds = new DataSet();
                _ds.ReadXml(reader);
                if (_dtUser != null && _dtUser.Rows.Count > 0 && _ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow rows in _dtUser.Rows)
                    {
                        if (_ds.Tables[0].Rows[0]["UserName"].ToString() == rows["UserName"].ToString() &&
                            _ds.Tables[0].Rows[0]["Passwords"].ToString() == rows["Passwords"].ToString())
                        {
                            Session["login"] = _ds.Tables[0].Rows[0]["login"];
                            Session["Users_Id"] = _ds.Tables[0].Rows[0]["Users_Id"];
                            Session["UserName"] = _ds.Tables[0].Rows[0]["UserName"];
                            Session["Employee_Id"] = _ds.Tables[0].Rows[0]["Employee_Id"];
                            Session["USer_Detail_Id"] = _ds.Tables[0].Rows[0]["USer_Detail_Id"];
                        }
                    }
                }
                else
                {
                    Session["login"] = false;
                    Session["Users_Id"] = string.Empty;
                    Session["UserName"] = string.Empty;
                    Session["Passwords"] = string.Empty;
                    Session["Employee_Id"] = string.Empty;
                    Session["USer_Detail_Id"] = string.Empty;
                    Session["IsUsed"] = string.Empty;
                    Session["IsActive"] = string.Empty;
                    Response.Redirect(@"../Login.aspx");
                }
            }
            else
            {
                Session["login"] = false;
                Session["Users_Id"] = string.Empty;
                Session["UserName"] = string.Empty;
                Session["Passwords"] = string.Empty;
                Session["Employee_Id"] = string.Empty;
                Session["USer_Detail_Id"] = string.Empty;
                Session["IsUsed"] = string.Empty;
                Session["IsActive"] = string.Empty;
                Response.Redirect(@"/Login.aspx");
            }
        }
        #endregion
    }
}